package com.woniu.controller;

import com.woniu.entity.User1;
import com.woniu.pojo.Power;
import com.woniu.pojo.User;
import com.woniu.service.UserService;
import com.woniu.utils.PasswordMd5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Source;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    UserService userService;
    //登录

    @ResponseBody
    @RequestMapping("login")
    public Map<String,Object> login(User user, String roleStatus, HttpSession httpSession){

        Map<String,Object> map=new HashMap<>();
        try {
            Map<String, Object> login = userService.login(user, roleStatus);
            User1 user1 = (User1) login.get("user");

            List<Power> tree= (List<Power>) login.get("tree");

            if(tree!=null&&tree.size()>0){

                httpSession.setAttribute("tree",tree);
            }

            if(user1.getUid()!=null&&user1.getUser().getUserId()!=null){

                httpSession.setAttribute("user",user1);


                map.put("result","ok");
            }else {
                map.put("result","error");
            }

        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();
        }

    return  map;
    }

    //主页面
    @RequestMapping("main")
    public String main(){
        return "WEB-INF/main";
    }
    //退出到登录页面
    @RequestMapping("quit")
    public String quit(HttpSession session){
        if(session.getAttribute("user")!=null){
            session.removeAttribute("user");
            if(session.getAttribute("tree")!=null){
                session.removeAttribute("tree");
            }
        }


        return "login";
    }
    @ResponseBody
    @RequestMapping("quitAjax")
    public  Map<String,Object> quitAjax(HttpSession httpSession){
        Map<String,Object> map=new HashMap<>();
        try {
            if(httpSession.getAttribute("user")!=null){
                httpSession.removeAttribute("user");
                if(httpSession.getAttribute("tree")!=null){
                    httpSession.removeAttribute("tree");
                }
            }
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();
        }

        return map;
    }
    @RequestMapping("updatePwd")
    public String updatePwd(){

        return "WEB-INF/updatePwd";
    }

    @ResponseBody
    @RequestMapping("isuserpwd")
    public Map<String,Object> isuserpwd(String userPwd,HttpSession session){
        Map<String,Object> map=new HashMap<>();
        try {
            User1 user = (User1) session.getAttribute("user");

            String pwd=null;
            if(userPwd!=null){
                try {
                    pwd= PasswordMd5.getMD5String(userPwd);

                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                    map.put("result","error");
                }
                if(pwd.equals(user.getUser().getUserPwd())){

                    map.put("result","ok");
                }else{
                    map.put("result","error");
                }

            }else {
                map.put("result","error");
            }
        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();
        }


        return map;
    }


}
